package com.atguigu.gulimall.order.config;

import com.atguigu.common.constant.MQConstant;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * 一个交换机绑定两个队列，其中一个队列时死信队列
 */
@Configuration
public class MyMQConfig {
    /**
     * 容器中的Queue、Exchange、Binding会自动创建（RabbitMQ没有的情况下）
     * 死信队列，不能被消费，只建立绑定关系，超时未支付的订单都会放到这个队列中
     *
     * @return
     */
    @Bean
    public Queue orderDeplayQueue() {
        /**
         * x-dead-letter-exchange:order-event-exchange
         * x-dead-letter-routing-key:order.release.order
         * x-message-ttl:60000
         */

        Map<String, Object> arguments = new HashMap<>();
        arguments.put("x-dead-letter-exchange", MQConstant.ORDER_EVENT_EXCHANGE);
        arguments.put("x-dead-letter-routing-key", MQConstant.ORDER_RELEASE_ORDER_KEY);
        arguments.put("x-message-ttl", MQConstant.ORDER_DELAY_X_MESSAGE_TTL); //1分钟,单位毫秒,哭着大喊这里的60000一定不要写成字符串类型，因为这个搞了一上午
        //String name, boolean durable, boolean exclusive, boolean autoDelete, Map<String, Object> arguments
        Queue queue = new Queue(MQConstant.ORDER_DELAY_QUEUE, true, false, false, arguments);
        return queue;
    }

    /**
     * 库存服务绑定该队列，进行解锁库存或关闭订单
     * 进入该队列的都是超时的或回滚的订单
     *
     * @return
     */
    @Bean
    public Queue orderReleaseOrderQueue() {
        Queue queue = new Queue(MQConstant.ORDER_RELEASE_ORDER_QUEUE, true, false, false);
        return queue;
    }

    /**
     * 秒杀下单队列，主要用于流量晓峰。
     * 秒杀成功立马发送一条消息，返回秒杀成功，然后订单服务去消费这条消息。
     *
     * @return
     */
    @Bean
    public Queue orderSeckillOrderQueue() {
        return new Queue(MQConstant.ORDER_SECKILL_ORDER_QUEUE, true, false, false);
    }

    /**
     * 交换机，与延时队列以及普通的订单队列绑定
     *
     * @return
     */
    @Bean
    public Exchange orderEventExchange() {
        //String name, boolean durable, boolean autoDelete, Map<String, Object> arguments
        return new TopicExchange(MQConstant.ORDER_EVENT_EXCHANGE, true, false);
    }

    /**
     * 创建交换机和延时队列的绑定关系
     *
     * @return
     */
    @Bean
    public Binding orderCreateOrderBinding() {
        return new Binding(MQConstant.ORDER_DELAY_QUEUE,
                Binding.DestinationType.QUEUE,
                MQConstant.ORDER_EVENT_EXCHANGE,
                MQConstant.ORDER_CTEATE_ORDER_KEY, null);
    }

    /**
     * 交换机和普通队列的绑定关系
     *
     * @return
     */
    @Bean
    public Binding orderReleaseOrderBinding() {
        return new Binding(MQConstant.ORDER_RELEASE_ORDER_QUEUE,
                Binding.DestinationType.QUEUE,
                MQConstant.ORDER_EVENT_EXCHANGE,
                MQConstant.ORDER_RELEASE_ORDER_KEY, null);
    }

    /**
     * 解锁库存队列和交换机进行绑定
     *
     * @return
     */
    @Bean
    public Binding orderReleaseOtherBinding() {
        return new Binding(MQConstant.STOCK_RELEASE_STOCK_QUEUE,
                Binding.DestinationType.QUEUE,
                MQConstant.ORDER_EVENT_EXCHANGE,
                MQConstant.ORDER_RELEASE_OTHER_LIKE_KEY, null);
    }


    /**
     * 秒杀队列和订单交换机进行绑定
     *
     * @return
     */
    @Bean
    public Binding orderSeckillOrderQueueBinding() {
        return new Binding(MQConstant.ORDER_SECKILL_ORDER_QUEUE,
                Binding.DestinationType.QUEUE,
                MQConstant.ORDER_EVENT_EXCHANGE,
                MQConstant.ORDER_SECKILL_ORDER_KEY,
                null
                );
    }
}
